package singleton;

import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import utils.MybatisUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class SingleRegisterFrame extends JFrame {
    // 使用懒汉单例模式中的双重检查锁
    private static SingleRegisterFrame registerInstance;

    private SingleRegisterFrame() {
    }

    private static SqlSession sqlSession = MybatisUtil.getSession();
    // 创建userMapper接口，然后通过反射来获取代理对象
    private static UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    private boolean check(String str) {
        return str == null || str.equals("");
    }

    public static SingleRegisterFrame getInstance() {

        if (registerInstance == null) {

            synchronized (SingleRegisterFrame.class) {

                if (registerInstance == null) {

                    registerInstance = new SingleRegisterFrame();
                    registerInstance.setTitle("注册界面");
                    registerInstance.setBounds(450, 250, 400, 170);
                    JPanel j1 = new JPanel();
                    JPanel j2 = new JPanel();
                    JPanel j3 = new JPanel();
                    JTextField userid = new JTextField(12);
                    JPasswordField userpwd = new JPasswordField(12);
                    JButton breg = new JButton("注册");
                    Container c = registerInstance.getContentPane();
                    c.setLayout(new GridLayout(4, 1));
                    j1.add(new JLabel("用户名"));
                    j1.add(userid);
                    c.add(j1);
                    j2.add(new JLabel("密码"));
                    j2.add(userpwd);
                    c.add(j2);
                    j3.add(breg);
                    c.add(j3);
                    registerInstance.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    registerInstance.setVisible(true);
                    registerInstance.setLocationRelativeTo(null);
                    breg.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String id = userid.getText();
                            String pwd = userpwd.getText();
                            if (registerInstance.check(id) || registerInstance.check(pwd)) {
                                JOptionPane.showMessageDialog(null, "输入不能为空!");
                            } else {
                                int cnt = userMapper.selectHasUser(id);
                                if (cnt > 0) {
                                    // 提示用户名已注册
                                    JOptionPane.showMessageDialog(null, "用户名已存在，请重新输入另一个用户名！");
                                } else {
                                    // 注册用户，添加一名用户
                                    User user = new User(id, pwd);
                                    cnt = userMapper.addUser(user);
                                    //注意提交事务
                                    sqlSession.commit();
                                    if (cnt > 0) {
                                        int res = JOptionPane.showConfirmDialog(null, "是否继续登陆", "注册成功！",
                                                JOptionPane.YES_NO_OPTION);
                                        if (res == JOptionPane.YES_OPTION) {
                                            System.out.println("继续登陆");
                                            registerInstance.setVisible(false);
                                            // 并且销毁当前stage，销毁时要先关闭会话资源
                                            sqlSession.close();
                                        } else {
                                            System.out.println("退出登录");
                                            System.exit(0);
                                        }
                                    } else {
                                        JOptionPane.showMessageDialog(null, "注册失败！");
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }
        return registerInstance;
    }
}
